//
// brief: 	设备服务
// author: 	yun
// data: 	2017-01-20
// desc: 	一个终端设备具有的基本功能 [登入|登出|心跳]
// version:	v0.9
//

package worker

import (
	"errors"
	"encoding/json"
	"time"
)

// 设备消息
type devMessage struct {
	Type 	string			`json:"type"`
	Data 	interface{}		`json:"data"`
}


// 设备登陆数据
type devLoginMessage struct {
	Id 			string		`json:"id"`
	Key 		string		`json:"key"`
	Software 	string		`json:"version"`
	Time 		string		`json:"time"`
}

// 登陆返回结果
type devLoginPost  struct {
	Type 	string		`json:"type"`
	State 	string		`json:"state"`
	Time 	string		`json:"time"`
}


// 心跳返回结果
type devHBPost struct {
	Type 	string		`json:"type"`
	Time 	string		`json:"time"`
}

//
// 设备服务
//
type SevDevice struct {
	Service
}

// 设备服务错误
var (
	SEV_DEV_ERR = errors.New("Data Error")
)

// 设备服务消息
const (
	SEV_DEV_ID 	= "dev"

	SEV_DEV_MSG_LOGIN	= "login"
	SEV_DEV_MSG_HEARTBEAT = "hb"

	SEV_DEV_LOGIN_FAIL = "fail"
	SEV_DEV_LOGIN_OK = "ok"

	SEV_DEV_NOTIFY_LOGIN	= "login"
	SEV_DEV_NOTIFY_LOGOUT = "logout"
)

//
// 设备消息
//
func (sev *SevDevice) Message(data interface{}) (err error) {
	err = SEV_DEV_ERR

	buf, ok := data.([]byte)
	if !ok {
		return
	}

	var msg devMessage
	e := json.Unmarshal(buf, &msg)
	if e != nil {
		return
	}

	if msg.Type == SEV_DEV_MSG_LOGIN {
		return sev.login(msg.Data)
	}

	if msg.Type == SEV_DEV_MSG_HEARTBEAT {
		return sev.heartbeat(msg.Data)
	}

	return nil
}


//
// 设备服务登陆
//
func (sev *SevDevice) login(info interface{}) (err error) {
	err = SEV_DEV_ERR

	buf, e := json.Marshal(info)
	if e != nil {
		return
	}

	var login devLoginMessage
	e = json.Unmarshal(buf, &login)

	var post_data = devLoginPost{
		Type: SEV_DEV_MSG_LOGIN,
		State: SEV_DEV_LOGIN_FAIL,
		Time: time.Now().Format("2006-01-02 15:04:05"),
	}

	post := SevPost{
		SevId: SEV_DEV_ID,
		Post: &post_data,
	}

	post_buf, _ := json.Marshal(post)

	dev := FindDevice(login.Id)
	if dev == nil {
		sev.Post <- post_buf
		return
	}

	// 比对密码
	if dev.Key != login.Key {
		sev.Post <- post_buf
		return
	}

	// 更新设备信息
	dev.Software = login.Software

	post_data.State = SEV_DEV_LOGIN_OK
	post_buf, _ = json.Marshal(post)
	sev.Post <- post_buf

	notify := Notify{
		SevId: SEV_DEV_ID,
		Notify: SevNotify{
			Date: dev.Id,
			Type: SEV_DEV_NOTIFY_LOGIN,
		},
	}

	notify_buf, _ := json.Marshal(notify)
	sev.Notify <- notify_buf

	return  nil
}

//
// 设备离线
// 根据设备的ID产生一条离校通知报文
//
func (sev *SevDevice) LogoutNotify(devId string) (notify_buf []byte, err error) {
	notify := Notify{
		DevId: devId,
		SevId: SEV_DEV_ID,
		Notify: SevNotify{
			Type: SEV_DEV_NOTIFY_LOGOUT,
			Date: nil,
		},
	}

	notify_buf, err = json.Marshal(notify)
	return
}

//
// 设备服务心跳
//
func (sev *SevDevice) heartbeat(info interface{}) (err error) {
	err = SEV_DEV_ERR

	_, e := json.Marshal(info)
	if e != nil {
		return
	}

	var post_data = devHBPost{
		Type: SEV_DEV_MSG_HEARTBEAT,
		Time: time.Now().Format("2006-01-02 15:04:05"),
	}

	post := SevPost{
		SevId: SEV_DEV_ID,
		Post: &post_data,
	}

	post_buf, _ := json.Marshal(post)
	sev.Post <- post_buf

	return nil
}






